/// <reference path="typings/index.d.ts" />
'use strict';
const path = require('path');
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
const run = require('run-sequence');
const rimraf = require('rimraf');
const glob = require('glob');

const bsync = require('browser-sync');
const reload = bsync.reload;

const app = {
    basePath: require('./bower.json').appPath || 'app',
    dist: './dist'
};

const paths = {
    scripts: [app.basePath + '/scripts/**/*.js'],
    styles: [app.basePath + '/styles/**/*.scss'],
    jsRequire: [
        app.basePath + '/bower_components/angular/angular.js',
        app.basePath + '/bower_components/angular-loader/angular-loader.js',
        app.basePath + '/bower_components/angular-mocks/angular-mocks.js',
        app.basePath + '/bower_components/angular-route/angular-route.js',
        app.basePath + '/bower_components/html5-boilerplate/dist/js/vendor/modernizr-2.8.3.min.js',
        app.basePath + '/bower_components/jquery/dist/jquery.min.js',
        app.basePath + '/bower_components/bootstrap/dist/js/bootstrap.min.js',
        app.basePath + '/bower_components/requirejs/require.js',
    ],
    cssRequire: [
        app.basePath + '/bower_components/bootstrap/dist/css/bootstrap.css',
        app.basePath + '/bower_components/html5-boilerplate/dist/css/main.css',
        app.basePath + '/bower_components/html5-boilerplate/dist/css/normalize.css'
    ],
};

gulp.task('clear', callback => {
    console.log('this is clear task............................');
    rimraf(app.dist + '/**/*', callback);
});

/**sass */
gulp.task('sass', () => {
    console.log('this is sass task..................');
    gulp.src(paths.styles)
        .pipe($.plumber())
        .pipe($.sass({
            // outputStyle: 'expanded'
            outputStyle: 'compressed'
        }))
        .pipe(gulp.dest('./dist/app/styles'))
        .pipe(reload({
            stream: true
        }));
});

gulp.task('watch:css', () => {
    gulp.watch(paths.styles, ['sass']);
});
gulp.task('watch:js', () => {
    gulp.watch('app/view/**/*.js', ['copy:routejs']);
});
gulp.task('watch:html', () => {
    // gulp.watch([app.basePath + '/**/*.js', app.basePath + '/**/*.html'], ['copy']);
    gulp.watch([app.basePath + '/view/**/*.html'], ['copy:html']);
});

/** */
gulp.task('watch:roothtml', () => {
    gulp.watch('./app/index.html', ['copy:roothtml']);
});
/** */
gulp.task('watch:rootjs', () => {
    gulp.watch('./app/*.js', ['copy:rootjs']);
});
/** */
gulp.task('copy:routejs', () => {
    glob.sync('app/view/**/*.js')
        .forEach(file => {
            gulp.src(file)
                .pipe($.plumber())
                .pipe(gulp.dest('dist/' + path.parse(file).dir))
                .pipe(reload({
                    stream: true
                }));
        });
});
gulp.task('copy:html', () => {
    glob.sync('app/view/**/*.html')
        .forEach(file => {
            gulp.src(file)
                .pipe($.plumber())
                .pipe(gulp.dest('dist/' + path.parse(file).dir))
                .pipe(reload({
                    stream: true
                }));
        });
});
gulp.task('copy:roothtml', () => {
    console.log('index.html changed ..................................');
    gulp.src('./app/index.html')
        .pipe($.plumber())
        .pipe(gulp.dest('dist/app/'))
        .pipe(reload({
            stream: true
        }))
});
gulp.task('copy:rootjs', () => {
    console.log('app.js changed .......................................');
    gulp.src('./app/*.js')
        .pipe($.plumber())
        .pipe(gulp.dest('dist/app/'))
        .pipe(reload({
            stream: true
        }))
});
/** */
gulp.task('copy:components', () => {
    gulp.src(app.basePath + '/components/**/*')
        .pipe(gulp.dest('./dist/app/components'));
    //    copy ico
    gulp.src(app.basePath + '/*.*')
        .pipe(gulp.dest('./dist/app/'));
});
/** */
gulp.task('copy:jsRequire', () => {
    paths.jsRequire.forEach(file => {
        gulp.src(file)
            // .pipe($.uglify())
            .pipe(gulp.dest('./dist/' + path.parse(file).dir))
    });
});
/** */
gulp.task('copy:cssRequire', () => {
    paths.cssRequire.forEach(file => {
        gulp.src(file)
            .pipe(gulp.dest('./dist/' + path.parse(file).dir))
    });
});
/** */
gulp.task('copy', () => {
    // content
    run(['copy:routejs',
        'copy:html',
        'copy:components',
        'copy:jsRequire',
        'copy:cssRequire',
        'copy:roothtml',
        'copy:rootjs',
    ], reload);
});

// 静态服务器
gulp.task('bserver', () => {
    bsync.init({
        server: {
            baseDir: "./app"
        }
    });
});

gulp.task('default', () => {
    run(['clear'], ['sass', 'copy'], [
        'bserver',
        'watch:css',
        'watch:js',
        'watch:html',
        'watch:roothtml',
        'watch:rootjs'
    ]);
    console.log('this is default task!!!');
});